import {observable, action, runInAction} from 'mobx';
import {Modal} from 'antd-mobile';

const operation = Modal.operation;

/**
 * mobx 状态管理类，所有的逻辑处理都在这里
 * */
class RepairInfoStore {
    @observable repairInfo; //报修信息
    @observable ownerInfo; //业主信息
    @observable staffInfo;

    @action
    setRepairInfo = (repairInfo) => {
        this.repairInfo = repairInfo;
    };

    @action
    queryOwnerInfo = () => {
        postJson("/comm/repair/queryOwnerInfo.x", {
            ownerCode: this.repairInfo.owner_code,
            myOwnerCode: ownerCode
        }, (data) => {
            console.log("data", data);
            runInAction(() => {
                this.ownerInfo = data.ownerInfo;
                this.staffInfo = data;
            });
        });
    };

    @action
    acceptance = (personLiableInfo) => {
        console.log('personLiableInfo', personLiableInfo)
        postJson('/comm/repair/saveTreatmentResult.x', personLiableInfo, () => {
            runInAction(() => {
                this.repairInfo.repair_per = personLiableInfo.repairPer;
                this.repairInfo.repair_per_phone = personLiableInfo.repairPerPhone;
                window.history.back();
            });
        });
    };

    @action
    assignAcceptance = (personLiableInfo) => {
        let orgIds = [];
        this.staffInfo.subRole.map(subRole => {
            orgIds.push(subRole.id)
        });
        postJson('/property/staff/getByOrgs.x', {
            orgIds: orgIds,
            staffType: 30
        }, (data) => {
            let operationArray = [];
            for (let i in data) {
                operationArray.push({
                    text: data[i].staffName,
                    onPress: () => this.acceptance({
                        id: personLiableInfo.id,
                        state: personLiableInfo.state,
                        ownerCode: personLiableInfo.ownerInfo ? personLiableInfo.ownerInfo.ownerCode : '',
                        repairPer: data[i].staffName,
                        repairPerPhone: data[i].phoneNum
                    })
                })
            }
            operation(operationArray);
        });
    };

    @action
    treatmentResult = (treatmentResult) => {
        postJson('/comm/repair/saveTreatmentResult.x', treatmentResult, () => {
            window.history.back();
        });
    }
}

export default new RepairInfoStore()